package com.dcc.inventory.servicesimp;

import com.dcc.inventory.entity.Auth;
import com.dcc.inventory.entity.User;
import com.dcc.inventory.mapper.AuthMapper;
import com.dcc.inventory.mapper.UserMapper;
import com.dcc.inventory.services.UserServices;
import com.dcc.inventory.utils.AuthUtils;
import com.dcc.inventory.utils.Md5Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Service
@Transactional
public class UserServicesImp implements UserServices {
    @Resource
    protected UserMapper userMapper;
    @Resource
    protected AuthMapper authMapper;
    @Override
    public Auth login(String email, String upwd, HttpServletRequest request) {
        String upwds = Md5Utils.MD5(upwd);
        HttpSession session = request.getSession();
        User user = userMapper.login(email, upwds);
        Auth auth1 = new Auth();
        if(user != null){
            if(user.getUser_id() > 0){
                session.setAttribute("user",user);
                Auth auth = new Auth();
                auth.setUser_email(user.getUser_email());
                auth.setAuth(AuthUtils.auth());
                authMapper.updateAuth(auth);
                session.setAttribute("auth",auth.getAuth());
                auth1 = authMapper.searchAuth(user.getUser_email());
            }
        }
        return auth1;
    }

    @Override
    public Auth simpleLogin(String email, String upwd,HttpServletRequest request) {
        String upwds = Md5Utils.MD5(upwd);
        HttpSession session = request.getSession();
        User user = userMapper.login(email, upwds);
        Auth auth1 = new Auth();
        if(user != null){
            if(user.getUser_id() > 0){
                session.setAttribute("index_user",user);
                Auth auth = new Auth();
                auth.setUser_email(user.getUser_email());
                auth.setAuth(AuthUtils.auth());
                authMapper.updateAuth(auth);
                session.setAttribute("auth",auth.getAuth());
                auth1 = authMapper.searchAuth(user.getUser_email());
            }
        }
        return auth1;
    }


    @Override
    public int updateTag(User user) {

        return userMapper.updateTag(user);
    }
}
